<?php
	require_once("header.php");
	require_once("functions.php");
	
	$errors['allset'] = isset($_POST['submit']) && ($_POST['username']=='' || $_POST['password']=='' 
		|| $_POST['confirm']=='' || $_POST['fullname']=='' || $_POST['birthdate']==''
		|| $_POST['email']=='' || $_POST['sex']=='');
		
	$errors['blank_username'] = false;
	$errors['blank_password'] = false;
	$errors['blank_confirm'] = false;
	$errors['blank_fullname'] = false;
	$errors['blank_birthdate'] = false;
	$errors['blank_email'] = false;
	$errors['blank_sex'] = false;
	if(isset($_POST['submit'])){		
		$errors['blank_username'] = $_POST['username']=='';	
		$errors['blank_password'] = $_POST['password']=='';
		$errors['blank_confirm'] = $_POST['confirm']=='';
		$errors['blank_fullname'] = $_POST['fullname']=='';
		$errors['blank_birthdate'] = $_POST['birthdate']=='';
		$errors['blank_email'] = $_POST['email']=='';
		$errors['blank_sex'] = !isset($_POST['sex']);
	}
	
	$formvalid = false;	
	$errors['username'] = 0;
	$errors['password'] = 0;
	$errors['confirm'] = 0;
	$errors['fullname'] = 0;
	$errors['birthdate'] = 0;
	$errors['email'] = 0;
        $errors['avatar'] = 0;
	if(isset($_POST['submit'])){
		$errors['username'] = validateUsername($_POST['username'],$_POST['password']);	
		$errors['password'] = validatePassword($_POST['password'],$_POST['username'],$_POST['email']);
		$errors['confirm'] = validateConfirm($_POST['confirm'],$_POST['password']);
		$errors['fullname'] = validateFullname($_POST['fullname']);
		$errors['birthdate'] = validateBirthdate($_POST['birthdate']);
		$errors['email'] = validateEmail($_POST['email'],$_POST['password']);
                $errors['avatar'] = validateAvatar($_FILES['avatar']['type']);
		
		$formvalid = !$errors['allset'] && !$errors['username'] && !$errors['password'] && !$errors['confirm']
				&& !$errors['fullname'] && !$errors['birthdate'] && !$errors['email'] && !$errors['avatar'] && !$errors['allset'];
	}
	
?>
	<div id="content">
		<div id="content-title">
			Form Registrasi
		</div>
		<div id="content-body">
		<?php if(!$formvalid) { ?>
			<form id="reg_form" action="" method="post" enctype="multipart/form-data">
				<ul>
					<?php if($errors['allset']){ ?>
					<li>						
						<div class="error_msg">Some fields are empty. Please fill out all the inquiries below!</div>
					</li>
					<?php } ?>
					<li>
						<div class="prompt">Username</div><input type="text" name="username" id="username" value="<?php if(isset($_POST['username'])) echo $_POST['username'];?>" onkeyup="checkUsernameEmail();">
						<?php if($errors['blank_username']){ ?>
						<div class="error_msg">Do not leave username blank!</div>
						<?php } else {
							switch($errors['username']){
								case 1 : ?>
								<div class="error_msg">Username must be more than 5 letters!</div>
								<?php break;
								case 2 : ?>								
								<div class="error_msg">Username must be different from password!</div>
								<?php break;
								case 3 : ?>
								<div class="error_msg">Username <?php echo $_POST['username']; ?> has been registered!</div>
								<?php break;
							}
						}
						?>
						<div class="error_msg" id="username_respond"></div>
					</li>
					<li>
						<div class="prompt">Password</div><input type="password" name="password">
						<?php if($errors['blank_password']){ ?>
						<div class="error_msg">Do not leave password blank!</div>
						<?php } else {
							switch($errors['password']){
								case 1 : ?>
								<div class="error_msg">Password min 8 length!</div>
								<?php break;
								case 2 : ?>								
								<div class="error_msg">Password must be different from username!</div>
								<?php break;
								case 3 : ?>
								<div class="error_msg">Password must be different from email!</div>
								<?php break;
							}
						}
						?>
					</li>
					<li>
						<div class="prompt">Confirm Password</div><input type="password" name="confirm">
						<?php if($errors['blank_confirm']){ ?>
						<div class="error_msg">Do not leave password confirmation blank!</div>
						<?php } else {
							switch($errors['confirm']){
								case 1 : ?>
								<div class="error_msg">Different from password!</div>
								<?php break;
							}
						}
						?>
					</li>
					<li>
						<div class="prompt">Full Name</div><input type="text" name="fullname" value="<?php if(isset($_POST['fullname'])) echo $_POST['fullname'];?>">
						<?php if($errors['blank_fullname']){ ?>
						<div class="error_msg">Do not leave full name blank!</div>
						<?php } else {
							switch($errors['fullname']){
								case 1 : ?>
								<div class="error_msg">Place your first and last name seperated by space</div>
								<?php break;
							}
						}
						?>
					</li>
					<li>
						<div class="prompt">Birthdate</div><input type="text" name="birthdate" value="<?php if(isset($_POST['birthdate'])) echo $_POST['birthdate'];?>">
						<?php if($errors['blank_birthdate']){ ?>
						<div class="error_msg">Do not leave birthdate blank!</div>
						<?php } else {
							switch($errors['birthdate']){
								case 1 : ?>
								<div class="error_msg">Ensure your date format YYYY-DD-MM!</div>
								<?php break;
								case 2 : ?>
								<div class="error_msg">Invalid date!</div>
								<?php break;
							}
						}
						?>
					</li>
					<li>
						<div class="prompt">Email</div><input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" onkeyup="checkUsernameEmail();">
						<?php if($errors['blank_email']){ ?>
						<div class="error_msg">Do not leave email blank!</div>
						<?php } else {
							switch($errors['email']){
								case 1 : ?>
								<div class="error_msg" id="error_email">
									Differ from password<br />
								</div>						
								<?php break;
								case 2 : ?>
								<div class="error_msg" id="error_email">
									Min 1 char before @<br />
									Min 1 char between @ and .<br />
									Min 2 char after .<br />
								</div>						
								<?php break;
								case 3 : ?>
								<div class="error_msg" id="error_email">
									E-mail has been registered<br />
								</div>
								<?php break;
							}
						}
						?>
						<div class="error_msg" id="email_respond"></div>
					</li>
					<li>
						<div class="prompt">Gender</div>
						<input type="radio" name="sex" value="male" <?php if(isset($_POST['sex']) && $_POST['sex']=='male') {?>checked="checked"<?php } ?>> Male
						<input type="radio" name="sex" value="female" <?php if(isset($_POST['sex']) && $_POST['sex']=='female') {?>checked="checked"<?php } ?>> Female
						<?php if($errors['blank_sex']){ ?>
						<div class="error_msg">Choose your gender!</div>
						<?php } ?>
					</li>
					<li>
						<div class="prompt">Avatar</div><input type="file" name="avatar" accept="image/jpeg">
						<?php
							switch($errors['avatar']){
								case 1 : ?>
								<div class="error_msg" id="error_avatar">Choose jpg/jpeg one!</div>
								<?php break;
							}
						?>				
					</li>
					<li>
						<div class="prompt">About Me</div><textarea rows="5" cols="40" name="aboutme" placeholder="Saya adalah seorang yang ..."></textarea>
					</li>
					<li>
						<div class="prompt"><input type="submit" value="Submit" name="submit"></div>
					</li>
				</ul>
			</form>
		<?php } else {
				require_once('db.php');
                                //This is the directory where images will be saved 
                                $target = "user_avatar/"; 
                                $target = $target . basename( $_FILES['avatar']['name']);
                                $pic=($_FILES['avatar']['name']);
//                                $extension = pathinfo($pic, PATHINFO_EXTENSION);
  //                              if($extension!='jpg' && $extension!='jpeg')                                
                                move_uploaded_file($_FILES['avatar']['tmp_name'], $target);
				if(connect_db()){                                    
					$sql = "INSERT INTO pengguna VALUES('".$_POST['username']."','".$_POST['email']."','".$_POST['password']."','".$_POST['fullname']."','".$_POST['sex']."','".$_POST['birthdate']."','".$_POST['aboutme']."','".$pic."');";
					if (!mysql_query($sql))
						printf('A database error occurred in processing your '.
						'submission.\nIf this error persists, please '.
						'contact admin@pobunesia.org');
					else{
						echo "Registration success!<br />";
                                                echo "Username : ".$_POST['username']."<br />";
                                                echo "Fullname : ".$_POST['fullname']."<br />";
                                                echo "Birthdate : ".$_POST['birthdate']."<br />";
                                                echo "E-mail : ".$_POST['email']."<br />";
                                                echo "Sex : ".$_POST['sex']."<br />";
                                        }
				}				
			}
		?>
		</div>
	</div>
<?php require_once("footer.php"); ?>
